package com.zlm.spider.processor;

import com.zlm.spider.entity.Item;
import com.zlm.spider.service.ItemService;
import com.zlm.spider.util.HttpUtils;
import com.zlm.spider.util.RedisUtils;
import lombok.extern.slf4j.Slf4j;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Element;
import org.seimicrawler.xpath.JXDocument;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import redis.clients.jedis.Jedis;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author Harbour
 * @date 2021-04-12 13:35
 */
@Component
@Slf4j
public class ItemProcessor {

    private static int totalTimes = 0;

    @Autowired
    private HttpUtils httpUtils;

    @Autowired
    private ItemService itemService;

    @Autowired
    ConfigurableApplicationContext context;

//    @Scheduled(fixedDelay = 1000 * 20) // 每次间隔10秒执行一次
    public void process() {
        String url = "https://search.jd.com/Search?keyword=%E4%B9%A6&wq=%E4%B9%A6&click=0";

        if (totalTimes > 200) {
//            context.close();
            int exitCode = SpringApplication.exit(context);
            System.exit(exitCode);
        }

        String spiderStartInfo = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss" + " 第 " + (totalTimes+1) + "次爬虫开始...")
                .format(new Date());
        log.info(spiderStartInfo);

        // 爬取网页，并打印信息
//        String uri = "&s=" + (totalTimes*29+1) + "&page=" + (totalTimes+1);
        String uri = "&page=" + (totalTimes+1);
        parseHtml(httpUtils.getHtml(url + uri));
        totalTimes ++;

        String spiderEndInfo = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss" + " 第 " + totalTimes + "次爬虫完成")
                .format(new Date());
        log.info(spiderEndInfo);
    }

    /**
     * 解析数据，并持久化到Mysql
     *
     * @param html 爬取的网页
     */
    private void parseHtml(String html) {

        /*
        XPath 信息
        //*[@id="J_goodsList"]/ul/li[1]
        //*[@id="J_goodsList"]/ul/li[30]
        */

        // todo： 1. 解析页面，获取对应数据
        JXDocument doc = JXDocument.create(Jsoup.parse(html));

        /*
        *  JD 页面设置了下拉刷新，每次只会加载30条数据， 因此这里循环30次
        */
        for (int i = 1; i <= 30; i++) {
            // 根据XPATH获取对应位置元素
            String xpath = "//*[@id=\"J_goodsList\"]/ul/li[" + i + "]";
            Object xPathElement = doc.selOne(xpath);// 获取该xpath路径下的所有element

            if (xPathElement instanceof Element) {
                // 对这个element进行转化
                Element e = (Element) xPathElement;
                String skuId = e.attr("data-sku");

                Jedis client = RedisUtils.getJedisClient();
                Long exist = client.sadd("sku-set", skuId);
                client.close();

                // skuId 为空的，一般都是广告产品，剔除掉；exist = 0 表示 数据已存在
                // 这里如果数据库内已经存在该数据，redis是读取不到的，还是会重复
                if (skuId != null && !skuId.equals("") && exist == 1L) {

                    Element titleElement = (Element) doc.selOne(xpath + "/div/div[3]/a/em");
                    String title = titleElement.text().replace("<font class=\"skcolor_ljg\">书</font>", "书");

                    Element priceElement = (Element) doc.selOne(xpath + "/div/div[2]/strong/i");
                    double price = Double.parseDouble(priceElement.text());

                    Element e1 = (Element) doc.selOne(xpath + "/div/div[1]/a/img");
                    String pic = "https:" + e1.attr("data-lazy-img")
                            .replace("/n9/", "/n1/");

                    // todo 2. 装配 Item 对象
                    Item item = new Item();
                    item.setSku(Long.parseLong(e.attr("data-sku")));
                    item.setSpu(Long.parseLong(e.attr("data-sku")));
                    item.setPic(httpUtils.getImage(pic));
//                    item.setPic(pic);
                    item.setUrl("https://item.jd.com/" + skuId + ".html");
                    item.setPrice(price);
                    item.setTitle(title);
                    item.setCreated(new Date());
                    item.setUpdated(item.getCreated());

                    // todo 3. 将数据持久化
                    itemService.save(item);
                }

            }

        }

    }

}

/*

<li data-sku="12177765" data-spu="12177765" ware-type="11" class="gl-item">
 <div class="gl-i-wrap">
  <div class="p-img">
   <a target="_blank" title="专为中国孩子量身打造的百科书，不告诉你什么是世界，只帮你打开看世界的眼睛。多位专家联合打造，只提供专业、精准的知识。让孩子在的知识储备量遥遥领先。 100册以上团购优惠联系电话4006186622" href="//item.jd.com/12177765.html" onclick="searchlog(1, '12177765','2','2','','flagsClk=2097626');"> <img width="" height="" data-img="1" data-lazy-img="//img10.360buyimg.com/n1/s200x200_jfs/t28795/275/590443672/272628/57150652/5bf7bed0N2d64ad9d.jpg">
    <div id="belt" class="p-act-sign J_belt_box sign-2017083101" style="background-image: url('https://img30.360buyimg.com/jgsq-productsoa/jfs/t1/124259/9/15926/7509/5f8ebab7E5c4a3431/175225e506cb248a.png');">
     <div class="sign-title ac">
      自营每满100减50
      <span class="sign-date">4.12-4.20</span>
     </div>
    </div> </a>
   <div data-lease="" data-catid="3399" data-venid="1000004108" data-presale="0"></div>
  </div>
  <div class="p-price">
   <strong class="J_12177765" data-done="1"> <em>￥</em><i>232.40</i> </strong>
  </div>
  <div class="p-name">
   <a target="_blank" title="专为中国孩子量身打造的百科书，不告诉你什么是世界，只帮你打开看世界的眼睛。多位专家联合打造，只提供专业、精准的知识。让孩子在的知识储备量遥遥领先。 100册以上团购优惠联系电话4006186622" href="//item.jd.com/12177765.html" onclick="searchlog(1, '12177765','2','1','','flagsClk=2097626');"> <em>国家地理儿童百科全<font class="skcolor_ljg">书</font>（套装共12册）</em> <i class="promo-words" id="J_AD_12177765">专为中国孩子量身打造的百科书，不告诉你什么是世界，只帮你打开看世界的眼睛。多位专家联合打造，只提供专业、精准的知识。让孩子在的知识储备量遥遥领先。 100册以上团购优惠联系电话4006186622</i> </a>
  </div>
  <div class="p-bookdetails">
   <span class="p-bi-store" onclick="searchlog(1,0,0,64)"> <a title="浙江教育出版社" href="/search?publisher=%E6%B5%99%E6%B1%9F%E6%95%99%E8%82%B2%E5%87%BA%E7%89%88%E7%A4%BE" target="_blank">浙江教育出版社</a> | </span>
   <span class="p-bi-date">2017-09</span>
  </div>
  <div class="p-commit">
   <strong><a id="J_comment_12177765" target="_blank" href="//item.jd.com/12177765.html#comment" onclick="searchlog(1, '12177765','2','3','','flagsClk=2097626');"></a></strong>
  </div>
  <div class="p-shopnum" data-dongdong="" data-selfware="1" data-score="5" data-reputation="99">
   <a class="curr-shop hd-shopname" target="_blank" onclick="searchlog(1,'1000004108',0,58)" href="//mall.jd.com/index-1000004108.html?from=pc" title="日知图书京东自营店">日知图书京东自营店</a>
  </div>
  <div class="p-icons" id="J_pro_12177765" data-done="1">
   <i class="goods-icons J-picon-tips J-picon-fix" data-idx="1" data-tips="京东自营，品质保障">自营</i>
  </div>
  <div class="p-operate">
   <a class="p-o-btn focus  J_focus" data-sku="12177765" href="javascript:;" onclick="searchlog(1, '12177765','2','5','','flagsClk=2097626')"><i></i>关注</a>
   <a class="p-o-btn addcart" href="//cart.jd.com/gate.action?pid=12177765&amp;pcount=1&amp;ptype=1" target="_blank" onclick="searchlog(1, '12177765','2','4','','flagsClk=2097626')" data-limit="0"><i></i>加入购物车</a>
  </div>
 </div> </li>


 */